package com.andy.module.network

import com.andy.network.base.BaseRetrofitClient
import com.andy.network.interceptor.CacheInterceptor
import com.andy.network.interceptor.ResponseBodyInterceptor
import com.andy.network.util.AndyDns
import com.andy.network.util.appContext
import okhttp3.Cache
import okhttp3.OkHttpClient
import java.io.File
import java.util.concurrent.TimeUnit

/**
 * @creator:LuoFan
 * @date:2022/9/29
 * @description:
 */
object RetrofitClient : BaseRetrofitClient() {

    val service by lazy { getService(SimpleApiService::class.java, SimpleApiService.SERVER_URL) }

    var headerInterceptor: HeadInterceptor = HeadInterceptor()

    var bodyInterceptor: ResponseBodyInterceptor = BodyInterceptor()

    override fun handleBuilder(builder: OkHttpClient.Builder) {
        builder.apply {
            //设置缓存配置 缓存最大10M
            cache(Cache(File(appContext.cacheDir, "cxk_cache"), 10 * 1024 * 1024))
            //添加Cookies自动持久化
//            cookieJar(cookieJar)
            //示例：添加公共heads 注意要设置在日志拦截器之前，不然Log中会不显示head信息
            addInterceptor(headerInterceptor)
            //添加解析容错
            addInterceptor(bodyInterceptor)
            //添加缓存拦截器 可传入缓存天数，不传默认7天
            addInterceptor(CacheInterceptor())
            //dnf缓存
            dns(AndyDns())
            //超时时间 连接、读、写
            connectTimeout(10, TimeUnit.SECONDS)
            readTimeout(10, TimeUnit.SECONDS)
            writeTimeout(10, TimeUnit.SECONDS)
        }
    }
}